package com.qudian.pay.schedule.controller;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qudian.pay.core.model.CommonResp;

public class BaseController {
	
	public Logger logger = LoggerFactory.getLogger(getClass());
	
	private static final int STATUS_BAD_REQUEST = 400;
	
	private static final int STATUS_SERVER_ERROR = 500;
	
	private static final String SUCCESS_MSG = "操作成功";
	
	private static final String FAIL_MSG = "操作失败";
	
	private static final String SERVER_ERROR_MSG = "服务器异常";
	
	public CommonResp<Object> success() {
		return success(SUCCESS_MSG, null);
	}
	
	public CommonResp<Object> success(String msg) {
		return success(msg, null);
	}
	
	public CommonResp<Object> success(String msg, Object data) {
		CommonResp<Object> resp = new CommonResp<Object>();
		resp.setData(data);
		resp.setMsg(msg);
		resp.setCode(0);
		return resp;
	}
	
	public CommonResp<Object> fail() {
		return fail(FAIL_MSG, null);
	}
	
	public CommonResp<Object> fail(String msg) {
		return fail(msg, null);
	}
	
	public CommonResp<Object> fail(String msg, Object data) {
		CommonResp<Object> resp = new CommonResp<Object>();
		resp.setData(data);
		resp.setMsg(msg);
		resp.setCode(1);
		return resp;
	}
	
	public CommonResp<Object> badRequest(String msg) {
		CommonResp<Object> resp = new CommonResp<Object>();
		resp.setData(null);
		resp.setMsg(msg);
		resp.setCode(400);
		return resp;
	}
	
	public CommonResp<Object> badRequest(String msg, Object data) {
		CommonResp<Object> resp = new CommonResp<Object>();
		resp.setData(data);
		resp.setMsg(msg);
		resp.setCode(STATUS_BAD_REQUEST);
		return resp;
	}
	
	public CommonResp<Object> error() {
		return error(SERVER_ERROR_MSG, null);
	}
	
	public CommonResp<Object> error(String msg) {
		return error(msg, null);
	}
	
	public CommonResp<Object> error(String msg, Object data) {
		CommonResp<Object> resp = new CommonResp<Object>();
		resp.setData(data);
		resp.setMsg(StringUtils.defaultIfEmpty(msg, SERVER_ERROR_MSG));
		resp.setCode(STATUS_SERVER_ERROR);
		return resp;
	}
}
